<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="http://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
</head>

<body>
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target=".my-box"></button>
  <div class="modal my-box" tabindex="-1">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">Modal title</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p>Modal body text goes here.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.min.js"></script>
  <script>
    // 1. 获取按钮
    const btn = document.querySelector('.btn')
    // 2. 获取弹框
    const myModal = document.querySelector('.my-box')
    // 3. 监听按钮的点击事件
    btn.addEventListener('click', () => {
      // 4. 显示弹框
      myModal.classList.add('show')
    })
    // 5. 监听弹框的点击事件
    myModal.addEventListener('click', (e) => {
      // 6. 阻止事件冒泡
      e.stopPropagation()
      // 7. 获取弹框的关闭按钮
      const closeBtn = document.querySelector('.btn-close')
      // 8. 监听关闭按钮的点击事件
      closeBtn.addEventListener('click', () => { })
    })

  </script>
</body>

</html>